﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DTO;
using DAO;
using BUS;

namespace QLBANSACH
{
    public partial class NV : Form
    {
        SqlConnection connection = DAO.sqlConnectionData.Hamketnoi();

        public NV()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                NV_DTO nv = new NV_DTO(Convert.ToInt32(textBox1.Text), textBox2.Text, textBox3.Text, textBox4.Text, Convert.ToInt32(textBox5.Text));
                NV_BUS.InsertNV(nv);
                dataGridView1.DataSource = NV_BUS.HienthiNV();
                databinding();
                MessageBox.Show("Thêm thành công!");
            }
            catch
            {
                MessageBox.Show("Thêm thất bại!");
                dataGridView1.DataSource = NV_BUS.HienthiNV();
                databinding();
            }
        }

        public void databinding()
        {
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add("Text", dataGridView1.DataSource, "ID_NV");
            textBox2.DataBindings.Clear();
            textBox2.DataBindings.Add("Text", dataGridView1.DataSource, "Ten_NV");
            textBox3.DataBindings.Clear();
            textBox3.DataBindings.Add("Text", dataGridView1.DataSource, "Username");
            textBox4.DataBindings.Clear();
            textBox4.DataBindings.Add("Text", dataGridView1.DataSource, "Password");
            textBox5.DataBindings.Clear();
            textBox5.DataBindings.Add("Text", dataGridView1.DataSource, "ID_Quyen");
        }

        private void NV_Load(object sender, EventArgs e)
        {
            connection.Open();
            dataGridView1.DataSource = NV_BUS.HienthiNV();
            databinding();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                NV_DTO nv = new NV_DTO(Convert.ToInt32(textBox1.Text), textBox2.Text, textBox3.Text, textBox4.Text, Convert.ToInt32(textBox5.Text));
                NV_BUS.UpdateNV(nv);
                dataGridView1.DataSource = NV_BUS.HienthiNV();
                databinding();
                MessageBox.Show("Sửa thành công!");
            }
            catch
            {
                MessageBox.Show("Sửa thất bại!");
                dataGridView1.DataSource = NV_BUS.HienthiNV();
                databinding();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                NV_DTO nv = new NV_DTO(Convert.ToInt32(textBox1.Text));
                NV_BUS.DeleteNV(nv);
                dataGridView1.DataSource = NV_BUS.HienthiNV();
                databinding();
                MessageBox.Show("Xóa thành công!");
            }
            catch
            {
                MessageBox.Show("Xóa thất bại!");
                dataGridView1.DataSource = NV_BUS.HienthiNV();
                databinding();
            }
        }
    }
}
